package com.sunhaojie.pos.front.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sunhaojie.pos.common.enums.ErrorCodeEnum;
import com.sunhaojie.pos.common.exception.BussinessRuntimeException;
import com.sunhaojie.pos.common.httpclient.HttpClientUtil;
import com.sunhaojie.pos.common.utils.DateFormatUtil;
import com.sunhaojie.pos.common.utils.RSAUtils;

@Service
public class ForwordBo {
	
	private static Logger logger = LoggerFactory.getLogger(ForwordBo.class);
	// private static final String gatewayHost = "http://120.92.101.137:8888";
	private static final String gatewayHost = "http://127.0.0.1:8888";
	private static final String appId = "common";
	private static final String privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMx9FkSv2+Qr4saZmbtaohI0jvisiK/mZW/w52CKw1W4VryVoJJ/o1kQymcrKvgByC/GxZxt6mKt5/wMbGTrM1W1s96urk6rNz7DKlXtmvfYbc+IenPGaESKeWAzoDHftFBjYIkL8NKN40l557r4R7Ss1Iop9Qxt9RYe41Fcmj2BAgMBAAECgYEApkzsOTwTxkJykTUFszqIJ+oag94842CqChmO8hKnvSTHVWMSTVIEVEC1eqW+sicw2Ln8p7U0MsBfs7mexYwKGCpECGaXrVgVmoK3lv4eNw4anq6zxmIsm7y3GinHNGuphXGNIfMN5rrWoKT/UUHd7Ch8dZI6azA2zabHtPLGg0kCQQD84mYTqFelDpEHEA77/T4AYdO4BUpMkTCzjgdaH0Miz6P8AnSUGUQ7t0qqQIEicWakogPtMNhSzFi0g6XDtY3nAkEAzwIMG7gpzfAamic8zQd++X9rZ7K0bDHAnP0po8TIB5e2KKAlM5bqbs+/d/T4EH9d+gPxXT7euMBn5F42INhcVwJBANXfmBs5/d4f4rJJd5LRDBYAprH073CwPrMtBLTIydvwJq1lWjlC4T1r+F7jLRP3Oqs0RwPckqf8IW2SBajurLMCQHubcFl47VVwAqJ7vX179WDLqfmgpsVYK/hR3vRQQ1TtTTMS9UPJ4LBMUrsxRpKrSxVIF26jZOCS2zZs3wtbxj8CQFf3HaORT0ylLB/7BTwmU8JAdixjWVEreY7+Gs0EJ4kwUsGwgPx60lDuVwn2tejgTrQptlEJMs8qG9v2SStuFuw=";
	
	public String forward(String path, String method, Map<String, Object> params) {
		logger.info(params.toString());
		path = path.replace("/web_front", "");
		Map<String, String> headers = new HashMap<String, String>();
		String url = gatewayHost  + path;
		Map<String, String> signParamsMap = new HashMap<String, String>();
		if(MapUtils.isNotEmpty(params)) {
			for(String key : params.keySet()) {
				Object object = params.get(key);
				if(object != null) {
					if (object instanceof String) {
						signParamsMap.put(key, (String) object);
					} else {
						signParamsMap.put(key, JSONObject.toJSONString(object));
					}
				}
			}
		}
		
		String timestamp = DateFormatUtil.format(new Date());
		signParamsMap.put("path", path);
		signParamsMap.put("app_id", appId);
		signParamsMap.put("timestamp", timestamp);
		String sign = null;
		try {
			sign = RSAUtils.sign(signParamsMap, privateKey);
		} catch (Exception e) {
			logger.error("签名错误", e);
			throw new BussinessRuntimeException(ErrorCodeEnum.SIGN_ERROR);
		}

		headers.put("app_id", appId);
		headers.put("sign", sign);
		headers.put("sign_type", "RSA");
		headers.put("timestamp", timestamp);
		
		return HttpClientUtil.execute(url, headers, params, method);
	}
}